# tasks/models.py
from django.db import models
from users.models import User
from tasks.models import Task  # 引入任务模型

class FocusSession(models.Model):
    user = models.ForeignKey(User, on_delete=models.CASCADE, related_name='focus_sessions')
    task = models.ForeignKey(Task, on_delete=models.CASCADE, null=True, blank=True, related_name='focus_sessions')  # 关联任务
    start_time = models.DateTimeField(auto_now_add=True)  # 会话开始时间
    end_time = models.DateTimeField(null=True, blank=True)  # 会话结束时间
    total_duration = models.IntegerField(null=True, blank=True)  # 总时长（秒）
    note = models.TextField(null=True, blank=True)  # 心得记录

    def __str__(self):
        return f"Session by {self.user} on {self.start_time}"


# tasks/models.py
class FocusLog(models.Model):
    session = models.ForeignKey(FocusSession, on_delete=models.CASCADE, related_name='logs')
    state = models.CharField(max_length=20, choices=[('started', 'Started'), ('paused', 'Paused'), ('resumed', 'Resumed'), ('ended', 'Ended'), ('abandoned', 'Abandoned')])  # 状态
    timestamp = models.DateTimeField(auto_now_add=True)  # 状态发生的时间
    extra_data = models.JSONField(null=True, blank=True)  # 额外数据（如设备信息、应用信息等）

    def __str__(self):
        return f"Log for session {self.session.id} at {self.timestamp}"
